﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace MT.CustomWindow
{
    /// <summary>
    /// ProgressWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressWindow : Window
    {
        public static double ProgressValue = 0;
        public static string ShowHint = string.Empty;
        public static string FinishedHint = string.Empty;

        public ProgressWindow()
        {
            InitializeComponent();
        }

        private ProgressWindow(Action act)
        {
            InitializeComponent();

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        TextBlock_Hint.Text = ShowHint;
                        progress.Value = ProgressValue;
                    });

                    if (ProgressValue == 100)
                        break;

                    Thread.Sleep(10);
                }

                Dispatcher.Invoke(() =>
                {
                    MessageBoxWindow.ShowMessage(FinishedHint, MessageBoxType.With_Ok, true);
                    ProgressValue = 0;
                    this.Close();
                });
            });
        }

        public static void Loading(Action act)
        {
            ProgressWindow win = new ProgressWindow(act);
            Task.Factory.StartNew(act);
            win.ShowDialog();
        }
    }
}
